package com.mm.service.impl;

import com.mm.dao.MessageMapper;
import com.mm.exception.ServiceException;
import com.mm.model.Message;
import com.mm.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 消息Service实现类
 *
 * @date 2018/7/19 15:43
 */
@Service(value = "messageService")
public class MessageServiceImpl implements MessageService {
	@Autowired
	private MessageMapper messageMapper;

	/**
	 * 保存消息
	 *
	 * @param message Message
	 */
	@Override
	public void saveMessage(Message message) {
		message.setAddTime(System.currentTimeMillis());
		messageMapper.insertMessage(message);
	}


	/**
	 * 更新消息
	 *
	 * @param message Message
	 */
	@Override
	public void updateMessage(Message message) {
		messageMapper.updateMessageById(message);
	}

	/**
	 * 获取消息
	 *
	 * @param messageId 消息ID
	 * @return Message
	 * @throws ServiceException
	 */
	@Override
	public Message getOneMessageById(Long messageId) throws ServiceException {
		if (messageId == null) {
			throw new ServiceException("消息ID不能为空");
		}
		return messageMapper.getOneMessageById(messageId);
	}

	/**
	 * 通过消息ID删除消息
	 *
	 * @param messageId 消息ID
	 * @throws ServiceException
	 */
	@Override
	public void deleteMessageById(Long messageId) throws ServiceException {
		if (messageId == null) {
			throw new ServiceException("消息ID不能为空");
		}
		messageMapper.deleteMessageById(messageId);
	}
}
